package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.domain.ZhwyDefaultchargesDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ZhwyDefaultcharges;
import com.ruoyi.service.IZhwyDefaultchargesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 默认收费项(隐藏)Controller
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/feeSettings/defaultcharges")
public class ZhwyDefaultchargesController extends BaseController {
    @Autowired
    private IZhwyDefaultchargesService zhwyDefaultchargesService;

    /**
     * 查询默认收费项(隐藏)列表
     */
    @PreAuthorize("@ss.hasPermi('feeSettings:defaultcharges:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZhwyDefaultcharges zhwyDefaultcharges) {
        startPage();
        List<ZhwyDefaultcharges> list = zhwyDefaultchargesService.selectZhwyDefaultchargesList(zhwyDefaultcharges);
        return getDataTable(list);
    }

    /**
     * 导出默认收费项(隐藏)列表
     */
    @PreAuthorize("@ss.hasPermi('feeSettings:defaultcharges:export')")
    @Log(title = "默认收费项(隐藏)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhwyDefaultcharges zhwyDefaultcharges) {
        List<ZhwyDefaultcharges> list = zhwyDefaultchargesService.selectZhwyDefaultchargesList(zhwyDefaultcharges);
        ExcelUtil<ZhwyDefaultcharges> util = new ExcelUtil<ZhwyDefaultcharges>(ZhwyDefaultcharges.class);
        util.exportExcel(response, list, "默认收费项(隐藏)数据");
    }

    /**
     * 获取默认收费项(隐藏)详细信息
     */
    @PreAuthorize("@ss.hasPermi('feeSettings:defaultcharges:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zhwyDefaultchargesService.selectZhwyDefaultchargesById(id));
    }

    /**
     * 新增默认收费项(隐藏)
     */
    @PreAuthorize("@ss.hasPermi('feeSettings:defaultcharges:add')")
    @Log(title = "默认收费项(隐藏)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZhwyDefaultcharges zhwyDefaultcharges) {
        return toAjax(zhwyDefaultchargesService.insertZhwyDefaultcharges(zhwyDefaultcharges));
    }

    /**
     * 修改默认收费项(隐藏)
     */
    @PreAuthorize("@ss.hasPermi('feeSettings:defaultcharges:edit')")
    @Log(title = "默认收费项(隐藏)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZhwyDefaultcharges zhwyDefaultcharges) {
        return toAjax(zhwyDefaultchargesService.updateZhwyDefaultcharges(zhwyDefaultcharges));
    }

    /**
     * 删除默认收费项(隐藏)
     */
    @PreAuthorize("@ss.hasPermi('feeSettings:defaultcharges:remove')")
    @Log(title = "默认收费项(隐藏)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(zhwyDefaultchargesService.deleteZhwyDefaultchargesByIds(ids));
    }
    /**
     * 根据type(出租出售)查询默认收费项(隐藏)列表
     */
//    @PreAuthorize("@ss.hasPermi('feeSettings:defaultcharges:list')")
    @Anonymous
    @GetMapping("/listByType/{id}")
    public AjaxResult listType(@PathVariable Long id) {
        List<ZhwyDefaultcharges> list = zhwyDefaultchargesService.findZhwyDefaultchargesByType(id);
        return success(list);
    }
    //保存默认收费项数据
    @Anonymous
    @PostMapping("/save")
    public AjaxResult save(@RequestBody ZhwyDefaultchargesDTO zhwyDefaultchargesDTO){
        List<ZhwyDefaultcharges> hireOrSellList = zhwyDefaultchargesDTO.getHireOrSellList();
        Long flag = zhwyDefaultchargesDTO.getFlag();
        //根据出租出售删除所有数据
        zhwyDefaultchargesService.deleteAll(flag);
        //添加所有数据
        hireOrSellList.forEach(s->{
            ZhwyDefaultcharges zhwyDefaultcharges = new ZhwyDefaultcharges(null,s.getChargeitemId(),flag);
            zhwyDefaultchargesService.insertZhwyDefaultcharges(zhwyDefaultcharges);
        });
////        System.out.println(zhwyDefaultchargesVo);
//        List<ZhwyDefaultcharges> list = zhwyDefaultchargesVo.getHireOrSellList();
//        Long flag = zhwyDefaultchargesVo.getFlag();
//        List<ZhwyDefaultcharges> addList = null;
//        List<ZhwyDefaultcharges> needFilterList = null;
//        //获取id为null和不为null的数据
//        list.forEach(item ->{
//            if (item.getId()!=null){
//                addList.add(item);
//            }else {
//                needFilterList.add(item);
//            }
//        });
//        if (flag == 0){
//            //出租
//            // 获取数据库中数据
//            List<ZhwyDefaultcharges> zhwyDefaultchargesList = zhwyDefaultchargesService.findZhwyDefaultchargesByType(flag);
//            // 使用 Stream 过滤并提取 id
//            List<Long> ids = zhwyDefaultchargesList.stream()
//                    .map(ZhwyDefaultcharges::getId) // 提取数据库中每个对象的 id
//                    .filter(id -> needFilterList.stream().noneMatch(item -> item.getId().equals(id))) // 过滤出不在请求列表中的 id
//                    .collect(Collectors.toList());
//
//            Long[] array = ids.toArray(new Long[0]);
////            System.out.println(Arrays.toString(array));
//            if(array.length != 0){
//                //删除数据
//                zhwyDefaultchargesService.deleteZhwyDefaultchargesByIds(array);
//            }
//        }
//
////        //随便获取一个数据的type,确定出租还是出售
////        Long type = list.get(0).getType();
//
//
//
//        return success(200);
////        // 输出结果
////        System.out.println("不同的 id:");
        return success(200);
    }
}
